﻿#include "iconvString.h"
#include "iconv.h"

//#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)  // 编译链接的时候指定静态库 
#ifdef WIN32
#pragma comment(lib,"libiconv.lib")  
#endif

iconv_t g2u_cd;
iconv_t u2g_cd;


void code_convert_init()
{
	g2u_cd = iconv_open("utf-8", "gbk");
	u2g_cd = iconv_open("gbk", "utf-8");
}

void code_convert_destroy()
{
	iconv_close(g2u_cd);
	iconv_close(u2g_cd);
}

std::string utf8togbk(char src[])
{
	std::string ans;
	int len = strlen(src)*2+1;
	char *dst = (char *)new char[len];
	if(dst == NULL)
		return ans;

	memset(dst, 0, len);
	char *in = src;
	char *out = dst;
	size_t len_in = strlen(src);
	size_t len_out = len;

	if ((iconv_t)-1 == u2g_cd)
	{
		delete[] dst;
		return ans;
	}

	int n = 0;

	const char *temp = src;
	const char **pin = &temp;
	char **pout = &out;
	n = iconv(u2g_cd, pin, &len_in, pout, &len_out);

	if(n<0)
	{

	}
	else
	{
		ans = dst;
	}

	delete [] dst;
	return ans;
}

std::string gbktoutf8(char src[])
{
	std::string ans;
	int len = strlen(src)*2+1;
	char *dst = (char *)new char[len];
	if(dst == NULL)
		return ans;

	memset(dst, 0, len);
	char *in = src;
	char *out = dst;
	size_t len_in = strlen(src);
	size_t len_out = len;

	if ((iconv_t)-1 == g2u_cd)
	{
		delete[] dst;
		return ans;
	}

	int n = 0;

	const char *temp = src;
	const char **pin = &temp;
	char **pout = &out;
	n = iconv(g2u_cd, pin, &len_in, pout, &len_out);

	if(n<0)
	{

	}
	else
	{
		ans = dst;
	}

	delete [] dst;
	return ans;
}

std::string utf8togbk(const char* src)
{
	char* temp = new char[strlen(src)+1];
	memcpy(temp, src, strlen(src)+1);
	std::string strgbk = utf8togbk(temp);
	delete [] temp;
	return strgbk;
}

std::string gbktoutf8(const char* src)
{
	char* temp = new char[strlen(src)+1];
	memcpy(temp, src, strlen(src)+1);
	std::string strgbk = gbktoutf8(temp);
	delete [] temp;
	return strgbk;
}

bool IsLegitimateChrName(const char* name)
{
	char* tempname = new char[strlen(name)+1];
	memcpy(tempname, name, strlen(name)+1);
	std::string strgbk = utf8togbk(tempname);
	delete [] tempname;

	unsigned char ch1,ch2;
	const char *p = strgbk.c_str();
	for( ; *p!='\0'; ++p )
	{
		ch1=*p;
		if( ch1&0x80 )
		{
			ch2=*p;
			if( ! (ch1 >=0x81 && ch1<=0xfe && ch2>=0x40 && ch2<=0xfe && ch2!=0x7f ) )
			{
				return false;
			}
			++p;
		}
		else
		{
			if( !( (ch1>='a' &&  ch1 <='z') ||  (ch1>='A' &&  ch1<='Z') || (ch1>='0' &&  ch1 <='9') ) )
			{
				if(ch1 <= 32 || ch1==33 || ch1==34 || ch1==37 || ch1==39 || ch1==64 || ch1==94 || ch1==127)
				{
					return false;
				}
			}

		}
	}
	return true;
}

bool IsLegitimateAccountName(const char* name)
{
	unsigned char ch1,ch2;
	const char *p = name;
	for( ; *p!='\0'; ++p )
	{
		ch1=*p;

		if( !( (ch1>='a' &&  ch1 <='z') ||  (ch1>='A' &&  ch1<='Z') || (ch1>='0' &&  ch1 <='9') ) )
		{
			return false;
		}
	}
	return true;
}

bool IsAllNum(const char* str)
{
	unsigned char ch1,ch2;
	const char *p = str;
	for( ; *p!='\0'; ++p )
	{
		ch1=*p;

		if( !(ch1>='0' &&  ch1 <='9') )
		{
			return false;
		}
	}
	return true;
}
